

import { Body, Controller, Get,Post,Query } from '@nestjs/common';
import { NewCategoryService } from 'src/services/new-category/new-category.service'
import { INewCateGory } from 'src/types/new_category.interface';
import HttpResponse from 'src/types/http.response.interface';

type UnionType = INewCateGory[] | INewCateGory;

function sleep(duration){
    return new Promise((resolve,reject)=>{
        setTimeout(resolve,duration)
    })
}


@Controller('newCategories')
export class NewCategoryController {
      //等价于上面的写法
      constructor(private readonly  newCategoryService:NewCategoryService){

      }
      @Get()  
      async findAll():Promise<HttpResponse<INewCateGory[]>>{
        console.log('here')
         const data = await this.newCategoryService.findService();
         if(Array.isArray(data)){
          return {
              success:true,
              message:"查询数据成功",
              data,
          }
         }
         else{
          return {
              success:false,
              message:"查询数据失败",
              data:[],
          }
         }
         
      }
      /**
     * 用来通过postman或者insomnia来导入数据
     * 在浏览器中输入http://localhost:9898/newCategories/importData
     */
    
    @Post('/create')
    //DTO Data Transfer Object 数据传输对象，当客户端把数据传送到后台的
    async create(@Body() requestParamsBody:UnionType) {
      

        const data:UnionType = await this.newCategoryService.importData(requestParamsBody);
        return {
            success:true,
            message:"数据导入成功",
            data
        };
    }

    @Get('/queryAllKindArea')
    //DTO Data Transfer Object 数据传输对象，当客户端把数据传送到后台的
    async queryAllKindArea() {
        const allKindListIdList =[
            "66238848d5f4c2ae75760486", // "手机专区"
            "66238848d5f4c2ae75760489", // "电脑专区"
            "66238848d5f4c2ae75760488", // "平板专区"
            "66238848d5f4c2ae75760487", // "穿戴专区"
            "66238848d5f4c2ae7576048b", // "音频专区"
        ];
      

        const data = await this.newCategoryService.queryAllKindAreaById(allKindListIdList);
        if(data && data.length>0){
            return {
                success:true,
                message:"数据查询成功",
                data
            };
        }
        else{
            return {
                success:false,
                message:"数据查询失败"
            };
        }
    }


    /**
     * 分页查询数据
     * @param page 
     * @param limit 
     * @returns 
     */
    @Get('/getAll')
    async getAllProductInfo(
        @Query('page') page:number=1, //查询的页码
        @Query('limit') limit:number=6  //每页展示的条数
    ):Promise<HttpResponse<INewCateGory[]>>{

        await sleep(3*1000); //等待3秒钟

        const data =   await this.newCategoryService.getAllProductInfo(page,limit);
        if(data && data.length>0){
            return {
                success:true,
                message:"数据查询成功",
                data
            };
        }
        else{
            return {
                success:false,
                message:"数据查询失败"
            };
        }
    }

    /**
     * 根据
     * @param categoryId 
     */
    @Get("/fetchProductListByCategoryId")
    async fetchProductListByCategoryId(@Query("categoryId") categoryId:string){
        console.log('前端传入的参数===',categoryId)
        const data =   await this.newCategoryService.fetchProductListByCategoryId(categoryId);
        if(Array.isArray(data) && data.length>0){
            return {
                success:true,
                message:"数据查询成功",
                data
            };
        }
        else{
            return {
                success:false,
                message:"数据查询失败"
            };
        }
    }
  
}



